package com.dhcc.sdc.base.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.base.entity.ContraindicationItemEntity;
import com.dhcc.sdc.base.entity.ContraindicationItemRuleEntity;
import com.dhcc.sdc.base.entity.ContraindicationEntity;
import com.dhcc.sdc.base.service.IContraindicationItemRuleService;
import com.dhcc.sdc.base.service.IContraindicationItemService;
import com.dhcc.sdc.base.service.IContraindicationService;
import com.dhcc.sds.standard.entity.SdsStandRuleBaseEntity;
import com.dhcc.sds.standard.service.ISdsStandRuleBaseService;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 禁忌症条目匹配规则
 *
 * @author zhangjin
 * @ClassName: ContraindicationItemRuleController
 * @Description: TODO
 * @date 2022-09-07 09:44:17
 */
@Controller
@RequestMapping("${sys.backendPath}/sdc/contraindicationitemrule")
public class ContraindicationItemRuleController extends BaseController {

    private String PREFIX = "/backend/sdc/contraindicationitemrule/";

    @Autowired
    private IContraindicationItemRuleService contraindicationItemRuleService;

    @Autowired
    private IContraindicationItemService contraindicationItemService;

    @Autowired
    private IContraindicationService contraindicationService;

    @Autowired
    private ISdsStandSourceService sdsStandSourceService;

    @Resource
    ISdsStandRuleBaseService sdsStandRuleBaseService;

    /**
     * 配规则列表首页
     */
    @RequestMapping("/to_index/{contraindicationId}/{itemId}")
    public String index(Model model, @PathVariable Long contraindicationId, @PathVariable Long itemId) {
        model.addAttribute("contraindicationId", contraindicationId);
        model.addAttribute("itemId", itemId);
        return PREFIX + "contraindicationitemrule.html";
    }

    /**
     * 跳转到新增规则页面
     */
    @RequestMapping("/to_addRule")
    public String toAdd(Model model) {
        JSONObject standSourceMap = sdsStandSourceService.getSourceMap();
        model.addAttribute("dataSourceMap", standSourceMap);
        return PREFIX + "contraindicationitemrule_add.html";
    }

    /**
     * 跳转到修改规则页面
     */
    @RequestMapping("/to_updateRule/{contraindicationItemRuleId}")
    public String toUpdate(@PathVariable Long contraindicationItemRuleId, Model model) {
        ContraindicationItemRuleEntity contraindicationItemRule = this.contraindicationItemRuleService.selectById(contraindicationItemRuleId);
        model.addAttribute("contraindicationItemRule", contraindicationItemRule);
        JSONObject sourceMap = sdsStandSourceService.getSourceMap();
        model.addAttribute("dataSourceMap", sourceMap);
        return PREFIX + "contraindicationitemrule_edit.html";
    }

    /**
     *
     */
    @RequestMapping("/getRule")
    @ResponseBody
    public Object getRule(@RequestParam(value = "id") Long contraindicationItemRuleId, Model model) {
        ContraindicationItemRuleEntity contraindicationItemRule = this.contraindicationItemRuleService.selectById(contraindicationItemRuleId);
        return contraindicationItemRule;
    }

    @RequestMapping("/isRepeat")
    @ResponseBody
    @BussinessLog(value = "判断条目规则是否重复")
    public Object isRepeat(@RequestParam String expCode) {
        List<String> codeList = contraindicationItemRuleService.selectCode(expCode);
        if (codeList.size() == 0) {
            return R.ok();
        } else
            return R.error("规则代码不可重复");
    }

    /**
     * 删除禁忌症条目匹配规则
     */
    @RequestMapping(value = "/deleteRule")
    @ResponseBody
    @BussinessLog(value = "删除禁忌症条目匹配规则")
    public Object delete(@RequestParam Long contraindicationItemRuleId) {
        this.contraindicationItemRuleService.deleteById(contraindicationItemRuleId);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    @BussinessLog(value = "保存禁忌症条目匹配规则")
    public Object save(ContraindicationItemRuleEntity contraindicationItemRule) {
        if (CommonUtil.isOneEmpty(contraindicationItemRule)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        if (contraindicationItemRule.getId() == null) {
            contraindicationItemRule.setRuleDesc(StringEscapeUtils.unescapeHtml(contraindicationItemRule.getRuleDesc()));
            contraindicationItemRule.setOperator1(StringEscapeUtils.unescapeHtml(contraindicationItemRule.getOperator1()));
            contraindicationItemRule.setOperator2(StringEscapeUtils.unescapeHtml(contraindicationItemRule.getOperator2()));
            contraindicationItemRuleService.insert(contraindicationItemRule);
        } else {
            contraindicationItemRule.setRuleDesc(StringEscapeUtils.unescapeHtml(contraindicationItemRule.getRuleDesc()));
            contraindicationItemRule.setOperator1(StringEscapeUtils.unescapeHtml(contraindicationItemRule.getOperator1()));
            contraindicationItemRule.setOperator2(StringEscapeUtils.unescapeHtml(contraindicationItemRule.getOperator2()));
            contraindicationItemRuleService.updateAllColumnById(contraindicationItemRule);
        }
        return R.ok();
    }


    /**
     * 获取条目的关联规则
     */
    @RequestMapping(value = "/selectRuleByExpression")
    @ResponseBody
    public Object selectRuleByExpression(@RequestParam Map<String, Object> params) {
//        List<ContraindicationItemRuleEntity> itemRuleList = contraindicationItemRuleService.selectByExpression(params);
//        List<SdsStandRuleBaseEntity> result = sdsStandRuleBaseService.selectDisplayRule(params);
//        return result;
        return sdsStandRuleBaseService.selectDisplayRule(params);
    }


    /**
     * 数据源取值
     * linkStandSubId linkStandSubId
     */
    @RequestMapping(value = "/getSourceFieldAndStandDic")
    @ResponseBody
    public Object getSourceFieldAndStandDic(@RequestParam Long sourceId, @RequestParam(required = false) Long linkStandSubId) {
        //输出字段列表
        JSONArray fieldMap = contraindicationItemRuleService.getSourceFieldMap(sourceId, linkStandSubId);
        Map result = new HashMap();
        result.put("fieldMap", fieldMap.toJSONString());

        return result;
    }


}
